//SPDX-FileCopyrightText: Copyright 2025-2025 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package gitcode_triger

import (
	"fmt"
	"testing"
)

const _PR_HOOK_DATA = `
{
    "changes": {
        "merge_params": {
            "current": "force_remove_source_branch: false\n",
            "previous": null
        },
        "patchset_locked": {
            "current": false,
            "previous": null
        },
        "merge_when_pipeline_succeeds": {
            "current": false,
            "previous": null
        },
        "iid": {
            "current": 1,
            "previous": null
        },
        "target_branch": {
            "current": "main",
            "previous": null
        },
        "created_at": {
            "current": "2025-02-07T10:05:38+08:00",
            "previous": null
        },
        "description": {
            "current": "update: 更新文件 README.md\n\n修改了项目描述\n\n",
            "previous": null
        },
        "close_issue_when_merge": {
            "current": true,
            "previous": null
        },
        "moderation_result": {
            "current": false,
            "previous": null
        },
        "source_project_id": {
            "current": 4654867,
            "previous": null
        },
        "title": {
            "current": "修改了项目描述",
            "previous": null
        },
        "current_patchset_id": {
            "current": 0,
            "previous": null
        },
        "source_branch": {
            "current": "main",
            "previous": null
        },
        "squash": {
            "current": false,
            "previous": null
        },
        "updated_at": {
            "current": "2025-02-07T10:05:39+08:00",
            "previous": null
        },
        "merge_status": {
            "current": "unchecked",
            "previous": null
        },
        "moderation_time": {
            "current": 0,
            "previous": null
        },
        "latest_merge_request_diff_id": {
            "current": 2806316,
            "previous": null
        },
        "id": {
            "current": 6517785,
            "previous": null
        },
        "state": {
            "current": "opened",
            "previous": null
        },
        "author_id": {
            "current": 571598,
            "previous": null
        },
        "target_project_id": {
            "current": 4654803,
            "previous": null
        }
    },
    "project": {
        "path_with_namespace": "devflow/test",
        "ssh_url": "git@gitcode.com:devflow/test.git",
        "description": "",
        "git_http_url": "https://gitcode.com/devflow/test.git",
        "git_ssh_url": "git@gitcode.com:devflow/test.git",
        "url": "git@gitcode.com:devflow/test.git",
        "http_url": "https://gitcode.com/devflow/test.git",
        "ci_config_path": null,
        "web_url": "https://gitcode.com/devflow/test",
        "avatar_url": "https://cdn-img.gitcode.com/ee/bc/4e4989e374bc76aa35d49d5217bd120e1b5d0e8ac0defb05f64d2b976b6276ed.png?time=1717491324683",
        "name": "test",
        "namespace": "devflow",
        "visibility_level": 0,
        "default_branch": "main",
        "id": 4654803,
        "homepage": "https://gitcode.com/devflow/test"
    },
    "git_commit_no": "",
    "virtual_merge_build": false,
    "git_branch": "",
    "repository": {
        "name": "test",
        "description": "",
        "visibility_level": 0,
        "git_http_url": "https://gitcode.com/devflow/test.git",
        "url": "git@gitcode.com:devflow/test.git",
        "git_ssh_url": "git@gitcode.com:devflow/test.git",
        "homepage": "https://gitcode.com/devflow/test"
    },
    "issues": [],
    "object_kind": "merge_request",
    "labels": [],
    "produce_random_id": "bff21a30e4df48a2887a5729d32d74cb",
    "extend_attributes": null,
    "event_type": "merge_request",
    "object_attributes": {
        "merge_when_pipeline_succeeds": false,
        "last_commit": {
            "author": {
                "name": "linksaas",
                "email": "panleiming@linksaas.pro"
            },
            "id": "40ba83e5810b3e21116ddc9b93b55dff37ca3395",
            "message": "update: 更新文件 README.md \n 测试pr\n\nSigned-off-by: linksaas <panleiming@linksaas.pro>",
            "url": "https://gitcode.com/devflow/test2/commits/detail/40ba83e5810b3e21116ddc9b93b55dff37ca3395",
            "timestamp": "2025-02-07T02:03:42Z"
        },
        "iid": 1,
        "merge_user_id": null,
        "milestone_id": null,
        "created_at": "2025-02-07T10:05:38+08:00",
        "description": "update: 更新文件 README.md\n\n修改了项目描述\n\n",
        "omega_attributes": null,
        "source": {
            "path_with_namespace": "devflow/test2",
            "ssh_url": "git@gitcode.com:devflow/test2.git",
            "description": "",
            "git_http_url": "https://gitcode.com/devflow/test2.git",
            "git_ssh_url": "git@gitcode.com:devflow/test2.git",
            "url": "git@gitcode.com:devflow/test2.git",
            "http_url": "https://gitcode.com/devflow/test2.git",
            "ci_config_path": null,
            "web_url": "https://gitcode.com/devflow/test2",
            "avatar_url": "https://cdn-img.gitcode.com/ee/bc/4e4989e374bc76aa35d49d5217bd120e1b5d0e8ac0defb05f64d2b976b6276ed.png?time=1717491324683",
            "name": "test2",
            "namespace": "devflow",
            "visibility_level": 0,
            "default_branch": "main",
            "id": 4654867,
            "homepage": "https://gitcode.com/devflow/test2"
        },
        "title": "修改了项目描述",
        "head_pipeline_id": null,
        "source_branch": "main",
        "target_branch_commit": {
            "author": {
                "name": "linksaas",
                "email": "panleiming@linksaas.pro"
            },
            "id": "7dc8d063cb96248b88bea40605b1cd7122cebbbc",
            "message": "update: 更新文件 README.md \n 测试提交\n\nSigned-off-by: linksaas <panleiming@linksaas.pro>",
            "url": "https://gitcode.com/devflow/test/commits/detail/7dc8d063cb96248b88bea40605b1cd7122cebbbc",
            "timestamp": "2025-02-07T01:58:20Z"
        },
        "need_review": false,
        "updated_at": "2025-02-07T10:05:39+08:00",
        "oldrev": "",
        "merge_commit_sha": null,
        "last_edited_at": null,
        "action": "open",
        "id": 6517785,
        "state": "opened",
        "last_edited_by_id": null,
        "assignee_id": null,
        "merge_params": {
            "force_remove_source_branch": false
        },
        "merge_error": null,
        "work_in_progress": false,
        "author": {
            "avatar_url": "https://cdn-img.gitcode.com/ee/bc/4e4989e374bc76aa35d49d5217bd120e1b5d0e8ac0defb05f64d2b976b6276ed.png?time=1717491324683",
            "name": "linksaas",
            "id": 571598,
            "email": "panleiming@linksaas.pro",
            "username": "linksaas"
        },
        "update_reason": "",
        "target_branch": "main",
        "source_project_id": 4654867,
        "url": "https://gitcode.com/devflow/test/merge_requests/1",
        "target": {
            "path_with_namespace": "devflow/test",
            "ssh_url": "git@gitcode.com:devflow/test.git",
            "description": "",
            "git_http_url": "https://gitcode.com/devflow/test.git",
            "git_ssh_url": "git@gitcode.com:devflow/test.git",
            "url": "git@gitcode.com:devflow/test.git",
            "http_url": "https://gitcode.com/devflow/test.git",
            "ci_config_path": null,
            "web_url": "https://gitcode.com/devflow/test",
            "avatar_url": "https://cdn-img.gitcode.com/ee/bc/4e4989e374bc76aa35d49d5217bd120e1b5d0e8ac0defb05f64d2b976b6276ed.png?time=1717491324683",
            "name": "test",
            "namespace": "devflow",
            "visibility_level": 0,
            "default_branch": "main",
            "id": 4654803,
            "homepage": "https://gitcode.com/devflow/test"
        },
        "time_estimate": null,
        "need_test": false,
        "total_time_spent": 0,
        "human_time_estimate": null,
        "merge_status": "unchecked",
        "reviewer_list": [],
        "human_total_time_spent": null,
        "updated_by_id": null,
        "assignee_list": [],
        "author_id": 571598,
        "target_project_id": 4654803,
        "conflict": false
    },
    "git_target_branch_commit_no": "7dc8d063cb96248b88bea40605b1cd7122cebbbc",
    "user": {
        "avatar_url": "https://cdn-img.gitcode.com/ee/bc/4e4989e374bc76aa35d49d5217bd120e1b5d0e8ac0defb05f64d2b976b6276ed.png?time=1717491324683",
        "name": "linksaas",
        "id": 571598,
        "email": "panleiming@linksaas.pro",
        "username": "linksaas"
    },
    "manual_build": false,
    "uuid": "1_d0961a82-e326-4393-87de-f3de3869d01e"
}
`

func TestExtractPrHookAttr(t *testing.T) {
	attrList, err := ExtractPrHookAttr([]byte(_PR_HOOK_DATA))
	if err != nil {
		t.Fatal(err)
		t.FailNow()
	}
	fmt.Println(attrList)
}
